package com.zr.bookcorner.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.zh.baselibrary.utils.TTSUtils;
import com.zr.bookcorner.R;
import com.zr.bookcorner.presenter.BorrowReturnDetailPresenter;
import com.zr.bookcorner.presenter.ManagerDetailPresenter;
import com.zr.bookcorner.service.VoiceEvent;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ManagerSubmitDialog extends Dialog {

    @BindView(R.id.tv_up)
    TextView tvUp;
    @BindView(R.id.tv_up_count)
    TextView tvUpCount;
    @BindView(R.id.tv_down)
    TextView tvDown;
    @BindView(R.id.tv_down_count)
    TextView tvDownCount;
    @BindView(R.id.tv_return)
    TextView tvReturn;
    @BindView(R.id.tv_return_count)
    TextView tvReturnCount;
    @BindView(R.id.tv_error)
    TextView tvError;
    @BindView(R.id.tv_error_count)
    TextView tvErrorCount;
    @BindView(R.id.ll_up)
    LinearLayout llUp;
    @BindView(R.id.ll_down)
    LinearLayout llDown;
    @BindView(R.id.ll_return)
    LinearLayout llReturn;
    @BindView(R.id.ll_error)
    LinearLayout llError;
    @BindView(R.id.tv_sure)
    TextView tvSure;
    /**
     * 空闲时间
     */
    private static final Long MILLISINFUTURE = (long) 121 * 1000;
    /**
     * 倒计时间隔
     */
    private static final Long COUNTDOWNINTERVAL = (long) 1 * 1000;

    private DialogListener mDialogListener;
    private CountDownTimer countTimer;
    public ManagerSubmitDialog(@NonNull Context context, int themeResId, DialogListener mDialogListener) {
        super(context, themeResId);
        this.mDialogListener = mDialogListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.submit_layout);
        ButterKnife.bind(this);
        //设置不可取消，点击其他区域不能取消
        setCancelable(false);
        setCanceledOnTouchOutside(false);
        initCountTimer();
    }

    public void setMessageInfo(String upBookInfo, String downBookInfo,String returnBookInfo, String errorBookInfo,int errorSize) {

        if (TextUtils.isEmpty(upBookInfo)) {
            llUp.setVisibility(View.GONE);
        } else {
            tvUp.setText(upBookInfo);
            tvUpCount.setText(upBookInfo.split("、").length + "(本)");
        }

        if (TextUtils.isEmpty(downBookInfo)) {
            llDown.setVisibility(View.GONE);
        } else {
            tvDown.setText(downBookInfo);
            tvDownCount.setText(downBookInfo.split("、").length + "(本)");
        }
        if (TextUtils.isEmpty(returnBookInfo)) {
            llReturn.setVisibility(View.GONE);
        } else {
            tvReturn.setText(returnBookInfo);
            tvReturnCount.setText(returnBookInfo.split("、").length + "(本)");
        }
        if (TextUtils.isEmpty(errorBookInfo)) {
            llError.setVisibility(View.GONE);
        } else {
            tvError.setText(Html.fromHtml(errorBookInfo));
            tvErrorCount.setText(errorSize+ "(本)");
        }

    }
    @OnClick({R.id.tv_mistake,R.id.tv_sure})
    public void onClick(View v) {
        if(v.getId() == R.id.tv_sure) {
            mDialogListener.submit();
           // TTSUtils.getInstance().speak(getContext().getString(R.string.manager_success));
            EventBus.getDefault().post(new VoiceEvent(getContext().getString(R.string.manager_success)));
        }
        else
            mDialogListener.mistakes();
        if(countTimer!=null)
            countTimer.cancel();
        cancel();
    }


    private void initCountTimer() {
        countTimer = new CountDownTimer(MILLISINFUTURE, COUNTDOWNINTERVAL) {
            @Override
            public void onTick(long millisUntilFinished) {
                tvSure.setText("确认(" + (millisUntilFinished / 1000) + "s)");
            }

            @Override
            public void onFinish() {
                if (isShowing()) {
                    mDialogListener.submit();
                    dismiss();
                }
            }
        };
        countTimer.start();
    }
    public interface DialogListener {
        void setPresenter(ManagerDetailPresenter bp) ;
        void submit();

        void mistakes();
    }
}
